<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js"></script>
    <style>
      #app {
        font-family: Avenir, Helvetica, Arial, sans-serif;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
        text-align: center;
        color: #2c3e50;
      }
    </style>
  </head>

  <body>
    <div id="app">
      <h1>watch的基础使用</h1>
      <h2>count的值:{{count}}</h2>
      <button @click="count++">点击累加count</button>
      <hr />
      <h2>和为:{{sun}}</h2>
      <button @click="sun=5">把和修改为5</button>
    </div>
    <script>
      Vue.config.productionTip = false;
      //实例化Vue
      const vm = new Vue({
        el: "#app",
        data() {
          return {
            count: 0,
            num1: 1,
            num2: 2,
          };
        },
        computed: {
          sun: {
            get() {
              return this.num1;
            },
            //计算属性可读可写(当获取sum的值的时候就调用getter函数,当对sum进行设置的时候,会调用setter函数)
            set(value) {
              console.log(value);
              
            },
          },
        },
        watch: {
          // watch可以监听data中的数据和计算属性,重点在监听
          count(newVal,oldVal) {
            console.log("我累加了",newVal,oldVal)

          },
          sun(){
            console.log("sun改变了,我可以做点什么");
          }
        },
      });
    </script>
  </body>
</html>
